<template>
  <div class="login">
       <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" class="login-form">
        <h3 class="title">漫舒听雨</h3>
          <el-form-item prop="username">
            <el-input v-model="ruleForm.username" type="text" size="large"  auto-complete="off"  placeholder="请输入用户名" :prefix-icon="UserFilled">
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input v-model="ruleForm.password" type="password" size="large"  auto-complete="off"  placeholder="请输入密码" :prefix-icon="Lock"> 
            </el-input>
          </el-form-item>
          
        <el-form-item prop="code">
        <el-input
          v-model="ruleForm.captchaCode"
          size="large"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          :prefix-icon="Key"
        >
         
        </el-input>
        
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>



      <el-checkbox v-model="ruleForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width:100%;"
          @click.prevent="submitForm(ruleFormRef)"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
         
        </el-form>
          <!--  底部  -->
    <div class="el-login-footer">
      <span>漫舒听雨.</span>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import {useRouter} from "vue-router"
import { ElMessage } from 'element-plus'
import type { ComponentSize, FormInstance, FormRules} from "element-plus";
import { UserFilled,Lock,Key } from '@element-plus/icons-vue'
import { useUserStore } from "../stores/users";
import type {RuleForm} from "../api/type.ts"
import {login,geCaptchaCode} from "@/api/index.ts"


import { setToken } from "@/api/auth";
const router=useRouter();
const u = useUserStore();

const formSize = ref<ComponentSize>("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
  username: '',
  password: '',
  rememberMe:false,
  captchaCode: '',
  captchaKey:''

});
const rules = reactive<FormRules<RuleForm>>({
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 1, max: 10, message: "长度必须在1到10位之间", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 1, max: 10, message: "长度必须在1到10位之间", trigger: "blur" },
  ],
  captchaCode: [
    { required: true, message: "请输入校验码", trigger: "blur" },
  ],

});

const loading = ref(false);
const register = ref(false);
const codeUrl = ref("");
//原始的先注释掉
/*const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
    } else {
      console.log("error submit!", fields);
    }
  });
};*/

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.resetFields();
};

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}));

//登录提交数据
const submitForm=(formEl:FormInstance|undefined)=>{
  if(!formEl){
    return;
  }
  //验证
  formEl.validate(async(valid)=>{
    if(valid){
      console.log("ok");
      //发送请求
      let res= await login(ruleForm);
      if(res.data && res.data.code && res.data.code===200){
        let {token} =res.data.data;//服务端返回token
        setToken(token);
        router.push('/home')
      }else{
        ElMessage.error(res.data.msg);
        getCode();
      }
    }else{
      console.log("error");
    }
  })
}

const getCode=async()=>{
  let res=await geCaptchaCode();
  if(res.data && res.data.code && res.data.code===200){
    let {captchaCode,captchaKey} =res.data.data;//服务端返回token
    codeUrl.value="data:image/gif;base64," +captchaCode;
    ruleForm.captchaKey=captchaKey;
  }
}

getCode();
</script>

<style lang='scss' scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/gulangyu.jpg");
  background-size: cover;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  .el-input {
    height: 40px;
    input {
      height: 40px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 40px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 40px;
  padding-left: 12px;
}

 

</style>
